package org.javaboy.storage.service;

import org.javaboy.storage.mapper.StorageMapper;
import org.javaboy.storage.model.Commodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class StorageService {
    @Autowired
    StorageMapper storageMapper;


    public Map<String, Object> deduce(Integer commodityCode, Integer count) {
        Commodity commodity = storageMapper.getCommodity(commodityCode);
        if (commodity == null) {
            throw new RuntimeException("商品不存在");
        }
        if (count > commodity.getCount()) {
            throw new RuntimeException("商品数量不足");
        }
        storageMapper.deduce(commodityCode, count);
        Map<String, Object> map = new HashMap<>();;
        map.put("result", "扣库存成功");
        return map;
    }
}
